package com.ruoyi.service.impl;

import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.client.CourseClient;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.domain.vo.OrdersVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.OrdersMapper;
import com.ruoyi.domain.Orders;
import com.ruoyi.service.IOrdersService;

/**
 * 订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-18
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private CourseClient courseClient;

    /**
     * 查询订单
     * 
     * @param ordersId 订单主键
     * @return 订单
     */
    @Override
    public Orders selectOrdersByOrdersId(Long ordersId)
    {
        return ordersMapper.selectOrdersByOrdersId(ordersId);
    }

    /**
     * 查询订单列表
     * 
     * @param orders 订单
     * @return 订单
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增订单
     * 
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders)
    {
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改订单
     * 
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders)
    {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单
     * 
     * @param ordersIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByOrdersIds(Long[] ordersIds)
    {
        return ordersMapper.deleteOrdersByOrdersIds(ordersIds);
    }

    /**
     * 删除订单信息
     * 
     * @param ordersId 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByOrdersId(Long ordersId)
    {
        return ordersMapper.deleteOrdersByOrdersId(ordersId);
    }

    @Override
    public List<OrdersVO> getOrdersWithCourse(Integer userid) {
        List<Orders> orders = ordersMapper.selOrdersByUserId(userid);
        System.out.println(orders);
        List<OrdersVO> ordersVOS = BeanUtil.copyToList(orders, OrdersVO.class);
        System.out.println(ordersVOS);
        R<List<Map<String,Object>>> r = courseClient.findALL();
        List<Map<String,Object>> courseList = r.getData();
        System.out.println(courseList);
        for (OrdersVO ordersVO : ordersVOS) {
            for (Map<String, Object> course : courseList) {
                Integer courseId = (Integer) course.get("courseId");
                if (ordersVO.getOrders1().equals(String.valueOf(courseId))) {
                    ordersVO.setCourseName((String) course.get("courseName"));
                    ordersVO.setCourseTime((Integer) course.get("courseTime"));
                    break;
                }
            }
        }
        return ordersVOS;
    }
}
